/*
* MIT License
*
* Copyright (c) 2020 liuchen1206
*
* This file is part of Open Media Server(https://github.com/liuchen1206/OpenMediaServer).
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:

* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.

* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<string.h>
#include<time.h>
#include"common.h"
#include"Node.h"
#include"VirtualNode.h"
#include"HashFun.h"
#include"MD5HashFun.h"
#include"ConHash.h"
#include "log4z.h"
#include "OMSServer.h"


using namespace std;
using namespace zsummer::log4z;

void getIP(char * IP)
{
	int a=0, b=0 , c=0, d=0;
	a = rand()%256;
	b = rand()%256;
	c = rand()%256;
	d = rand()%256;
	char aa[4], bb[4], cc[4], dd[4];
	oms::itoa(a, aa, 10);
	oms::itoa(b, bb, 10);
	oms::itoa(c, cc, 10);
	oms::itoa(d, dd, 10);
	strcpy(IP, aa);
	strcat(IP,".");
	strcat(IP,bb);
	strcat(IP,".");
	strcat(IP,cc);
	strcat(IP,".");
	strcat(IP,dd);
}

int main(int argc, char** argv)
{
    //start log
    ILog4zManager::getRef().setLoggerOutFile(LOG4Z_MAIN_LOGGER_ID, false);
    ILog4zManager::getRef().start();
    ILog4zManager::getRef().setLoggerLevel(LOG4Z_MAIN_LOGGER_ID,LOG_LEVEL_TRACE);

	OMS_ERROR eErr = OMS_ERR_FAILD;
	if ((eErr = COMSServer::Instance()->Init()) != OMS_ERR_SUCCESS)
	{
		LOGFMTI("Faild to call COMSServer::Init() with error(%s)", Error2String(eErr));
		exit(-1);
	} 
    LOGFMTI("oms start...");


	srand(time(0));
	freopen("out.txt","r",stdin);
	
	CHashFun * func = new CMD5HashFun();
	CConHash * conhash = new CConHash(func);

	CNode * node1 = new CNode("machineA", 50, const_cast<char*>("10.3.0.201"));
	CNode * node2 = new CNode("machineB", 80, const_cast<char*>("10.3.0.202"));
	CNode * node3 = new CNode("machineC", 20, const_cast<char*>("10.3.0.203"));
	CNode * node4 = new CNode("machineD", 100, const_cast<char*>("10.3.0.204"));

	conhash->addNode_s(node1);
	conhash->addNode_s(node2);
	conhash->addNode_s(node3);
	conhash->addNode_s(node4);

//	node1->setData("99999999");
	
	int ans1 ,ans2 ,ans3 ,ans4;
	ans1=ans2=ans3=ans4=0;
	
	char object[100];
	CNode * node ;

	conhash->delNode_s(node2);
	for(int i =0;i<30;i++)
	{
	//	getIP(object);
	//	cout<<object<<endl;
		cin>>object;
		node = conhash->lookupNode_s(object);
		if(node!=NULL)
		{
			cout<<object<<"----->\t"<<node->getIden()<<" \t "<<(char *)node->getData()<<endl;
			if(strcmp(node->getIden(),"machineA")==0) ans1++;
			if(strcmp(node->getIden(),"machineB")==0) ans2++;
			if(strcmp(node->getIden(),"machineC")==0) ans3++;
			if(strcmp(node->getIden(),"machineD")==0) ans4++;
		}
	}

	cout<<"Total test cases : "<<ans1+ans2+ans3+ans4<<endl;
	cout<<"Map to MachineA : "<<ans1<<endl;
	cout<<"Map to MachineB : "<<ans2<<endl;
	cout<<"Map to MachineC : "<<ans3<<endl;
	cout<<"Map to MachineD : "<<ans4<<endl;
	fclose(stdin);
	return 0;
}
